﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_103 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = ReportViewer1;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\SalesRpt_103.rdlc";

            strFilePath = "report/rdlc/SalesRpt_103.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            BillBll bll = new BillBll();
            DataSet ds;
            ds = bll.SalesRpt_103(sqlWhere);
            
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ds1_vw_SalesBillByProduct";

            DataTable dt = ds.Tables[0];
            DataTable dttmp;
            DataRow dr;
            DataRow drtmp;

            
            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_103(sqlWhere, "");
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "900合计：";
            dr["Mon1Natsum"] = drtmp["Mon1Natsum"];
            dr["Mon2Natsum"] = drtmp["Mon2Natsum"];
            dr["Mon3Natsum"] = drtmp["Mon3Natsum"];
            dr["Mon4Natsum"] = drtmp["Mon4Natsum"];
            dr["Mon5Natsum"] = drtmp["Mon5Natsum"];
            dr["Mon6Natsum"] = drtmp["Mon6Natsum"];
            dr["Mon7Natsum"] = drtmp["Mon7Natsum"];
            dr["Mon8Natsum"] = drtmp["Mon8Natsum"];
            dr["Mon9Natsum"] = drtmp["Mon9Natsum"];
            dr["Mon10Natsum"] = drtmp["Mon10Natsum"];
            dr["Mon11Natsum"] = drtmp["Mon11Natsum"];
            dr["Mon12Natsum"] = drtmp["Mon12Natsum"];
            dr["TotalNatsum"] = drtmp["TotalNatsum"];
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_103(sqlWhere, " and isOwner='1'");
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "901自产产品小计：";
            dr["Mon1Natsum"] = drtmp["Mon1Natsum"];
            dr["Mon2Natsum"] = drtmp["Mon2Natsum"];
            dr["Mon3Natsum"] = drtmp["Mon3Natsum"];
            dr["Mon4Natsum"] = drtmp["Mon4Natsum"];
            dr["Mon5Natsum"] = drtmp["Mon5Natsum"];
            dr["Mon6Natsum"] = drtmp["Mon6Natsum"];
            dr["Mon7Natsum"] = drtmp["Mon7Natsum"];
            dr["Mon8Natsum"] = drtmp["Mon8Natsum"];
            dr["Mon9Natsum"] = drtmp["Mon9Natsum"];
            dr["Mon10Natsum"] = drtmp["Mon10Natsum"];
            dr["Mon11Natsum"] = drtmp["Mon11Natsum"];
            dr["Mon12Natsum"] = drtmp["Mon12Natsum"];
            dr["TotalNatsum"] = drtmp["TotalNatsum"];
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_103(sqlWhere, " and isOwner='0'");
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "902代理产品小计：";
            dr["Mon1Natsum"] = drtmp["Mon1Natsum"];
            dr["Mon2Natsum"] = drtmp["Mon2Natsum"];
            dr["Mon3Natsum"] = drtmp["Mon3Natsum"];
            dr["Mon4Natsum"] = drtmp["Mon4Natsum"];
            dr["Mon5Natsum"] = drtmp["Mon5Natsum"];
            dr["Mon6Natsum"] = drtmp["Mon6Natsum"];
            dr["Mon7Natsum"] = drtmp["Mon7Natsum"];
            dr["Mon8Natsum"] = drtmp["Mon8Natsum"];
            dr["Mon9Natsum"] = drtmp["Mon9Natsum"];
            dr["Mon10Natsum"] = drtmp["Mon10Natsum"];
            dr["Mon11Natsum"] = drtmp["Mon11Natsum"];
            dr["Mon12Natsum"] = drtmp["Mon12Natsum"];
            dr["TotalNatsum"] = drtmp["TotalNatsum"];
            dt.Rows.Add(dr);
            
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();

        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and ((companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" and CategoryCode in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where categoryGUID in(select categoryGUID from Cms_sys_UserProductCategory where UserID ='").Append(base.CurrentUserID.ToString()).Append("')))");
            sb.Append(" or sales ='").Append(base.CurrentUserEntity.UserName.ToString()).Append("'");
            sb.Append(" or sales in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
            
            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and companyname = '" + ddlCompany.SelectedValue.ToString() + "'");
            }
            sb.Append(" and iYear = ").Append(ddlBudgetYear.SelectedValue);
            sb.Append(" and CustomerCode not in(select customerCode from cms_Bg_InnerCustomer)"); 

            

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
